<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############



$keyname = explode('-', get('blockId'));
if(count($keyname)>1){
    // loading block from stored in database
    $blockid = $DB->escape(get('blockId'));

    $numRows = $DB->countRows($CONFIG->dbprefix.'blocks', "id = '{$blockid}'");
    if($numRows ==1){
        $workspaceid = $DB->oneValue($CONFIG->dbprefix.'blocks', 'workspace_id', "id = '{$blockid}'");
    }else{
        $workspaceid = $USER->workspace;
    }

    $block = new JFX_Block;
    $block->loadById($blockid, $workspaceid, true, true);

    if(!is_object($block)){
        die('Error: could not load block');
    }

    $res = JFX::arrayToObject($block->getDetails());
    $res->details = JFX::arrayToObject($block->getDetails());


    $res->elements = JFX::ObjectToArray($block->getElements(true));
/*
echo '<pre>';
var_dump($res);
echo '</pre>';
exit;
*/

    JFX_GetBlockDetails_PrepareSubBlockDetails($res, $block);

    echo json_encode($res);
    exit;
}else{
    // loading new block from database
    $keyname = explode('_', $keyname[0]);
    $action = $keyname[1];
    $keyname = $keyname[0];
    $module = JFX::registry('JFX_Module_'.$keyname);
    $blockDetails = $module->getDetails($action);
    $blockDetails = JFX::arrayToObject($blockDetails);
    $blockDetails->parent_id = get('pageid');
    $blockDetails->id = $keyname.'_'.$action;
    $blockDetails->module_action = $action;
    $blockDetails->page_id = get('pageid');
    echo json_encode($blockDetails);
    exit;
}








function JFX_GetBlockDetails_PrepareSubBlockDetails(&$res, &$block){
    
    foreach($block->elements as $k=>$v){
        $details = $v->getDetails();
        $res->elements[$k]['details'] = $details;
        JFX_GetBlockDetails_PrepareSubBlockDetails($res->elements[$k], $v);
    }
}



